package com.epfresh.supplier.adapter;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.api.widget.MsgView;
import com.epfresh.api.widget.tablayout.utils.UnreadMsgUtils;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.Category;

import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;


public class CategoryListViewBinder extends ItemViewBinder<Category, CategoryListViewBinder.ViewHolder> implements View.OnClickListener {

    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.list_item_change_price, parent, false);
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull Category post) {

        int position = getPosition(holder);

        holder.setData(post);

        holder.itemView.setTag(R.id.item_key_position,position);
        holder.itemView.setTag(R.id.item_key_status,post);
        holder.itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {


        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer){
            int position = (int) tag;
            Object o = v.getTag(R.id.item_key_status);


            getAdapter().notifyDataSetChanged();
            if (onRecyclerViewItemClickListener != null && o instanceof Category){
                onRecyclerViewItemClickListener.onItemClick(v,position,o);
            }
        }
    }



    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvTitle;
        View vFlag;

        MsgView msgView;

        ViewHolder(@NonNull View itemView) {
            super(itemView);

            tvTitle = (TextView) itemView.findViewById(R.id.tv_city);
            vFlag = itemView.findViewById(R.id.v_flag);
            msgView = (MsgView) itemView.findViewById(R.id.msg);

        }


        void setData(Category post) {

            Category data = post;

            if (data.isSelect()) {
                vFlag.setVisibility(View.VISIBLE);
                itemView.setBackgroundResource(R.color.white);
                tvTitle.setTextColor(Color.parseColor("#e83a2b"));
            } else {
                itemView.setBackgroundColor(Color.TRANSPARENT);
                tvTitle.setTextColor(Color.parseColor("#434343"));
                vFlag.setVisibility(View.GONE);
            }

            tvTitle.setText(data.getName());

            String key = data.getKey();
            String id = data.getId();
            if ("|".equals(key) && (id == null || "".equals(id))){
                Log.e("cnt",post.getCnt()+"-------------");
                if (post.getCnt() > 0) {
                    UnreadMsgUtils.show(msgView, 0, true, true);
                    UnreadMsgUtils.setSize(msgView, LocalDisplay.dp2px(9));
                }else {
                    msgView.setVisibility(View.GONE);
                }
            }else {
                UnreadMsgUtils.showLitter(msgView,data.getCnt(),false,true);
            }

        }
    }







}
